<?php


namespace Mslynn\Customer\Block;


use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Block\Widget\AbstractWidget;
use Magento\Framework\Api\ArrayObjectSearch;

class Dob extends AbstractWidget
{
    /**
     * Constants for borders of date-type customer attributes
     */
    const MIN_DATE_RANGE_KEY = 'date_range_min';

    const MAX_DATE_RANGE_KEY = 'date_range_max';
    protected $month = '';
    protected $day = '';
    protected $year = '';
    /**
     * Date inputs
     *
     * @var array
     */
    protected $_dateInputs = [];

    /**
     * @var \Magento\Framework\View\Element\Html\Date
     */
    protected $dateElement;

    /**
     * @var \Magento\Framework\Data\Form\FilterFactory
     */
    protected $filterFactory;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Customer\Helper\Address $addressHelper
     * @param CustomerMetadataInterface $customerMetadata
     * @param \Magento\Framework\View\Element\Html\Date $dateElement
     * @param \Magento\Framework\Data\Form\FilterFactory $filterFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Helper\Address $addressHelper,
        CustomerMetadataInterface $customerMetadata,
        \Magento\Framework\View\Element\Html\Date $dateElement,
        \Magento\Framework\Data\Form\FilterFactory $filterFactory,
        array $data = []
    )
    {
        $this->dateElement = $dateElement;
        $this->filterFactory = $filterFactory;
        parent::__construct($context, $addressHelper, $customerMetadata, $data);
    }

    /**
     * @return void
     */
    public function _construct()
    {
        parent::_construct();
        $this->setTemplate('dob.phtml');
    }

    /**
     * @return bool
     */
    public function isEnabled()
    {
        $attributeMetadata = $this->_getAttribute('dob');
        return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
    }

    /**
     * @return bool
     */
    public function isRequired()
    {
        $attributeMetadata = $this->_getAttribute('dob');
        return $attributeMetadata ? (bool)$attributeMetadata->isRequired() : false;
    }

    /**
     * @param string $date
     * @return $this
     */
    public function setDate($date)
    {


        $date = explode('-', $date);

        if ($date&&count($date)>2) {

            $this->day = $date[2];
            $this->month = $date[1];
            $this->year = $date[0];
        }
        return $this;
    }

    public function getDefaultDay()
    {
        return $this->day;
    }

    public function getDefaultMonth()
    {
        return $this->month;
    }

    public function getDefaultYear()
    {
        return $this->year;
    }

    /**
     * Return Data Form Filter or false
     *
     * @return \Magento\Framework\Data\Form\Filter\FilterInterface|false
     */
    protected function getFormFilter()
    {
        $attributeMetadata = $this->_getAttribute('dob');
        $filterCode = $attributeMetadata->getInputFilter();
        if ($filterCode) {
            $data = [];
            if ($filterCode == 'date') {
                $data['format'] = $this->getDateFormat();
            }
            $filter = $this->filterFactory->create($filterCode, $data);
            return $filter;
        }
        return false;
    }

    /**
     * Apply output filter to value
     *
     * @param string $value
     * @return string
     */
    protected function applyOutputFilter($value)
    {
        $filter = $this->getFormFilter();
        if ($filter) {
            $value = $filter->outputFilter($value);
        }
        return $value;
    }


    /**
     * Return label
     *
     * @return \Magento\Framework\Phrase
     */
    public function getLabel()
    {
        return __('Date of Birth');
    }


    /**
     * Return id
     *
     * @return string
     */
    public function getHtmlId()
    {
        return 'dob';
    }

    public function getMonth()
    {
        return [
            [
                'option' => 'Month',
                'value' => '',
            ],
            [
                'option' => 'Jan.',
                'value' => '01'
            ],
            [
                'option' => 'Feb.',
                'value' => '02'
            ],
            [
                'option' => 'Mar.',
                'value' => '03'
            ],
            [
                'option' => 'Apr.',
                'value' => '04'
            ],
            [
                'option' => 'May.',
                'value' => '05'
            ],
            [
                'option' => 'Jun.',
                'value' => '06'
            ],
            [
                'option' => 'Jul.',
                'value' => '07'
            ],
            [
                'option' => 'Aug.',
                'value' => '08'
            ],
            [
                'option' => 'Sep.',
                'value' => '09'
            ],
            [
                'option' => 'Oct.',
                'value' => '10'
            ],
            [
                'option' => 'Nov.',
                'value' => '11'
            ],
            [
                'option' => 'Dec.',
                'value' => '12'
            ],
        ];


    }

    public function getDay()
    {
        $day = [[
            'option' => 'Day',
            'value' => ''
        ]];
        for ($i = 1; $i <= 31; $i++) {
            array_push($day, [
                'option' => $i,
                'value' => $i
            ]);
        }
        return $day;
    }

    public function getYear()
    {
        $year = [[
            'option' => 'Year',
            'value' => ''
        ]];
        for ($i = 2015; $i >= 1940; $i--) {
            array_push($year, [
                'option' => $i,
                'value' => $i
            ]);
        }
        return $year;
    }

}
